{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Wildcard Agent\n",
    "Use wildcard to dynamically select the right tool to use based on the user's request."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install agentsjson openai"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Setup API keys"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "OPENAI_API_KEY=\"<your-openai-api-key>\"\n",
    "RESEND_API_KEY=\"your-resend-api-key\"\n",
    "WILDCARD_API_KEY=\"your-wildcard-api-key\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Setup Authentication"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "from agentsjson.core.models.auth import ApiKeyAuthConfig, AuthType\n",
    "\n",
    "# API Key config for Resend\n",
    "resend_auth = ApiKeyAuthConfig(\n",
    "    type=AuthType.API_KEY,\n",
    "    key_value=RESEND_API_KEY,\n",
    "    key_name=None,\n",
    "    key_prefix=None\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Setup your agent Toolkit\n",
    "If you need to use multiple tools, you can add them to the toolkit."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "toolkit = {\n",
    "    \"resend\": resend_auth,\n",
    "}\n",
    "\n",
    "def find_auth(agents_json):\n",
    "    api = agents_json.sources[0].id\n",
    "    return toolkit[api]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Wildcard Tool Selection\n",
    "Find the right tool to use for the given task."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note: For resend you need to first create a valid domain.\n",
    "You can do this from your resend dashboard. Or you can query the wildcard agent to do this: \"Create a new domain in resend called 'wild-card.ai'\"\n",
    "\n",
    "You may need some additional manual steps to add the domain to MX records in your DNS provider, there's no API for this."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [],
   "source": [
    "query = \"Create a new domain in resend called 'wild-card.ai'\"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "from agentsjson.core.models.schema import AgentsJson\n",
    "\n",
    "\n",
    "def find_tool(query):\n",
    "    result = requests.get(\n",
    "        \"https://api.wild-card.ai/search\",\n",
    "        params={\n",
    "            \"query\": query,\n",
    "            \"collection_id\": \"c58a5915-7b28-49b1-a28d-d81ffc6f63ea\"\n",
    "        },\n",
    "        headers={\"X-API-Key\": WILDCARD_API_KEY}\n",
    "    )\n",
    "    \n",
    "    result = result.json()\n",
    "    if result.get(\"error\"):\n",
    "        raise ValueError(\"Error: \", result.get(\"error\"))\n",
    "    \n",
    "    return AgentsJson(**result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "agents_json = find_tool(query)\n",
    "agents_json"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Add the result to LLM to generate arguments"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [],
   "source": [
    "from openai import OpenAI\n",
    "from agentsjson.core.models.auth import BearerAuthConfig, AuthType\n",
    "from agentsjson import ToolFormat, execute, get_tools\n",
    "\n",
    "def run_agent(query, agents_json):\n",
    "    # Get the tool definition for the selected action\n",
    "    tools = get_tools(agents_json, ToolFormat.OPENAI)\n",
    "\n",
    "    client = OpenAI(api_key=OPENAI_API_KEY)\n",
    "    response = client.chat.completions.create(\n",
    "        model=\"gpt-4o\",\n",
    "        messages=[\n",
    "        {\n",
    "            \"role\": \"system\", \n",
    "            \"content\": \"You are a helpful assistant that completes tasks.\"\n",
    "        },\n",
    "        {\n",
    "            \"role\": \"user\", \n",
    "            \"content\": query\n",
    "        }],\n",
    "        tools=tools\n",
    "    )\n",
    "    return response\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "response = run_agent(query)\n",
    "response"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Execute the chosen action with authentication"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Execute the chosen action with authentication\n",
    "\n",
    "auth = find_auth(agents_json)\n",
    "result = execute(agents_json, response, ToolFormat.OPENAI, auth)\n",
    "result"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Try another query"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "query2 = \"Send an email to kaushik@wild-card.ai from contact@wild-card.ai with the subject 'Hello from Wild Card'\"\n",
    "\n",
    "\n",
    "agents_json2 = find_tool(query2)\n",
    "response2 = run_agent(query2, agents_json2)\n",
    "auth2 = find_auth(agents_json2)\n",
    "result2 = execute(agents_json2, response2, ToolFormat.OPENAI, auth2)\n",
    "result2\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "newenv2",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
